package to3;

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import java.awt.Dimension;
import java.awt.Toolkit;

public class TankenFrame extends javax.swing.JFrame {

    private KlantenLijst klantenLijst;
    private String selectedKlantnr; //deprecated: gebruik de methode getKlantnr van het selectedKlant object 
    private Klant selectedKlant;
    private int euro95Aantal, dieselAantal, gasAantal;

    public TankenFrame() {

        initComponents();
        setVisible(true);

        ////////SCHERM IN MIDDEN////////
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = this.getSize();

        int windowX = Math.max(0, (screenSize.width - windowSize.width) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);

        this.setLocation(windowX, windowY);
        ///////////////////////////////

        this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
        selectedKlantLabel.setText(selectedKlantnr);

        klantenLijst = new KlantenLijst(); //maak nieuwe KlantenLijst, laad direct alle klanten in
        try {
            readFile3();
        } catch (IOException ex) {
            Logger.getLogger(TankenFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btn_Reserveer = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        btn_Close = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        selectedKlantLabel = new javax.swing.JLabel();
        btn_SelecteerKlant = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setFocusTraversalPolicyProvider(true);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Tanken");

        btn_Reserveer.setText("Betaal");
        btn_Reserveer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ReserveerActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "euro 95", "diesel", "gas" }));

        btn_Close.setText("Hoofdmenu");
        btn_Close.setPreferredSize(new java.awt.Dimension(164, 46));
        btn_Close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CloseActionPerformed(evt);
            }
        });

        jTextField1.setText("Klantnummer");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel7.setText("geselecteerde klant:");

        selectedKlantLabel.setText("geen");

        btn_SelecteerKlant.setText("selecteer klant");
        btn_SelecteerKlant.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SelecteerKlantActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn_Close, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(125, 125, 125)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(btn_SelecteerKlant)
                                        .addGap(78, 78, 78)
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(selectedKlantLabel))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(223, 223, 223)
                                        .addComponent(jLabel1))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(278, 278, 278)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btn_Reserveer)))
                        .addGap(0, 94, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(selectedKlantLabel)
                    .addComponent(btn_SelecteerKlant))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(75, 75, 75)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Reserveer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 367, Short.MAX_VALUE)
                .addComponent(btn_Close, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_CloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CloseActionPerformed
        hoofdMenu hM = new hoofdMenu();
        this.dispose();
    }//GEN-LAST:event_btn_CloseActionPerformed

    public void updateLiters(String type, int liter) {
        try {
            readFile3();
        } catch (IOException ex) {
            Logger.getLogger(TankenFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (type.equals("euro 95")) {
            euro95Aantal = euro95Aantal - liter;
        }
        if (type.equals("diesel")) {
            dieselAantal = dieselAantal - liter;
        }
        if (type.equals("gas")) {
            gasAantal = gasAantal - liter;
        }
        try {
            writeFile();
        } catch (IOException ex) {
            Logger.getLogger(TankenFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void btn_ReserveerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ReserveerActionPerformed
        
        boolean doorgaan = false;
        if (selectedKlant != null) { //kijk of de klant wel is geselecteerd
            if (jTextField2.getText().matches("[0-9]{1,3}")) {
                if (jComboBox1.getSelectedItem().toString().equals("euro 95")) {
                    if (euro95Aantal > Integer.parseInt(jTextField2.getText())) {
                        doorgaan = true;
                    }
                }else if (jComboBox1.getSelectedItem().toString().equals("diesel")) {
                    if (dieselAantal > Integer.parseInt(jTextField2.getText())) {
                        doorgaan = true;
                    }
                } else if (jComboBox1.getSelectedItem().toString().equals("gas")) {
                    if (gasAantal > Integer.parseInt(jTextField2.getText())) {
                        doorgaan = true;
                    }
                }
                if (doorgaan == true) {
                    BetaalScherm bS = new BetaalScherm(selectedKlant);
                    List<Dienst> tbList = new ArrayList<>();
                    TankBeurt tb = new TankBeurt(jComboBox1.getSelectedItem().toString(), Integer.parseInt(jTextField2.getText()));
                    tb.setKlantnr(selectedKlantnr);
                    tbList.add(tb);
                    bS.addList(tbList);
                    bS.setReturnScreen("tanken");
                    this.dispose();
                }else{
                    JOptionPane.showMessageDialog(null, "er is niet genoeg brandstof", "FOUT", JOptionPane.ERROR_MESSAGE);
                }
            } 
            else{
            JOptionPane.showMessageDialog(null, "Je moet wel iets invullen!", "FOUT", JOptionPane.ERROR_MESSAGE);
        }
        }else{
            JOptionPane.showMessageDialog(null, "Eerst een klant invullen", "FOUT", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btn_ReserveerActionPerformed
    private void btn_SelecteerKlantActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_SelecteerKlantActionPerformed
        if (klantenLijst.checkNr(jTextField1.getText())) { //zelfde check als voorheen; bestaat het klanten nummer in de klantenlijst?
            selectedKlantnr = jTextField1.getText(); //deprecated zie declaratie commentaar
            selectedKlantLabel.setText(selectedKlantnr);
            selectedKlant = klantenLijst.getKlant(selectedKlantnr);
        } else {
            JOptionPane.showMessageDialog(null, "Klant niet bekend!", "FOUT", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btn_SelecteerKlantActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void writeFile() throws IOException {
        FileWriter fw = new FileWriter("brandstofLiters.txt");
        PrintWriter pw = new PrintWriter(fw);
        pw.print(euro95Aantal + ", " + dieselAantal + ", " + gasAantal);
        pw.close();
        fw.close();
    }

    private void readFile3() throws IOException {
        FileReader fr = new FileReader("brandstofLiters.txt");
        BufferedReader br = new BufferedReader(fr);

        while (true) {
            String regel = br.readLine();
            if (regel == null) {
                break;
            }
            Scanner sc = new Scanner(regel);
            sc.useDelimiter("\\s*,\\s*");
            euro95Aantal = Integer.parseInt(sc.next());
            dieselAantal = Integer.parseInt(sc.next());
            gasAantal = Integer.parseInt(sc.next());
            sc.close();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Close;
    private javax.swing.JButton btn_Reserveer;
    private javax.swing.JButton btn_SelecteerKlant;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JLabel selectedKlantLabel;
    // End of variables declaration//GEN-END:variables
}
